package com.hyyt.shopjsp.wxmg.util;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.tuckey.web.filters.urlrewrite.utils.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class WxMenuUtil {
	/**
	 * 日志
	 */
	public static Log logger = Log.getLog(WxMenuUtil.class);
	/**
	 * 创建微信自定义菜单
	 * @author 
	 * @return String
	 */
	public static String createWxMenu(String access_token,  String json){
		String url = WxStaticKeyUtil.WX_MENU_CREATE_URL + access_token;
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		try {
			json = new String(json.getBytes("utf-8"), "iso8859-1");
			StringEntity stringEntity = new StringEntity(json);
			stringEntity.setContentEncoding("iso8859-1");
			stringEntity.setContentType("application/json; encoding=iso8859-1");
			post.setEntity(stringEntity);
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = res.getEntity();
				String stringJson = EntityUtils.toString(entity, "utf-8");
				return stringJson;
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	/**
	 * 创建微信自定义菜单
	 * @author LQS
	 * @return Map
	 */
	public static Map createMenu(String appId,String appSecret,String menuJsonInfo) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		StringBuffer bufferRes = new StringBuffer();
		// 获取access_token
		String access_token = WxBasicUtil.getAccessToken(appId, appSecret);
		try {
			//menuInfo=new String(menuInfo.getBytes(),"UTF-8");
			URL createUrl = new URL(WxStaticKeyUtil.WX_MENU_CREATE_URL + access_token);
			HttpURLConnection conn = (HttpURLConnection) createUrl.openConnection();
			// 连接超时
			conn.setConnectTimeout(25000);
			// 读取超时 --服务器响应比较慢，增大时间
			conn.setReadTimeout(25000);
			HttpURLConnection.setFollowRedirects(true);
			// 请求方式
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
			conn.setRequestProperty("Referer", WxStaticKeyUtil.WX_API_URL);
			conn.connect();
			// 获取URLConnection对象对应的输出流
			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
			// 发送请求参数
//			logger.info("menuJsonInfo="+menuJsonInfo);
			out.write(menuJsonInfo);
			out.flush();
			out.close();
			InputStream in = conn.getInputStream();
			BufferedReader read = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String valueString = null;
			while ((valueString = read.readLine()) != null) {
				bufferRes.append(valueString);
			}
//			logger.info(bufferRes.toString());
			in.close();
			if (conn != null) {
				conn.disconnect();
			}
		} catch (Exception e) {
			logger.error(e);
		}
		jsonMap = WxBasicUtil.parseJsonStringToMap(bufferRes.toString());
		return jsonMap;
	}
	
	/**
	 * 删除微信自定义菜单
	 * @author LQS
	 * @return Map
	 */

	public static Map<String, String> deleteMenu(String access_token) {
		/**
		 * { "access_token":
		 * "40uwe8TWe0clJFQDG5rslHZsWgTasuoK7dwkd3UQogWHhbXfez5ThhQ5C5kjshD6mBv8hsABlOi_BTcS5NmcYi0NjrHKqsLYc-4jAJOAU5ZPGhlOfyfcVr3THBWHuiSP4Rfhq0IdbQEiLc4Sc6Zt3g"
		 * , "expires_in":7200 }
		 */
		StringBuffer bufferRes = new StringBuffer();
		try {
			String deleteURL = WxStaticKeyUtil.WX_MENU_DELETE_URL + access_token;
			URL realUrl = new URL(deleteURL);
			HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
			// 连接超时
			conn.setConnectTimeout(25000);
			// 读取超时 --服务器响应比较慢，增大时间
			conn.setReadTimeout(25000);
			HttpURLConnection.setFollowRedirects(true);
			// 请求方式
			conn.setRequestMethod("GET");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
			conn.setRequestProperty("Referer", WxStaticKeyUtil.WX_API_URL);
			conn.connect();
			// 获取URLConnection对象对应的输出流
			InputStream in = conn.getInputStream();
			BufferedReader read = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String valueString = null;
			while ((valueString = read.readLine()) != null) {
				bufferRes.append(valueString);
			}
			in.close();
			if (conn != null) {
				conn.disconnect();
			}
		} catch (Exception e) {
			logger.error(e);
		}
		Map<String, String> jsonMap = WxBasicUtil.parseJsonStringToMap(bufferRes.toString());
		return jsonMap;
	}

	/**
	 * 获取微信自定义菜单
	 * @author LQS
	 * @return String
	 */
	public static String getMenu(String access_token) {
		/**
		 * { "access_token":
		 * "40uwe8TWe0clJFQDG5rslHZsWgTasuoK7dwkd3UQogWHhbXfez5ThhQ5C5kjshD6mBv8hsABlOi_BTcS5NmcYi0NjrHKqsLYc-4jAJOAU5ZPGhlOfyfcVr3THBWHuiSP4Rfhq0IdbQEiLc4Sc6Zt3g"
		 * , "expires_in":7200 }
		 */
		StringBuffer bufferRes = new StringBuffer();
		try {
			String getURL = WxStaticKeyUtil.WX_MENU_GET_URL + access_token;
			URL realUrl = new URL(getURL);
			HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
			// 连接超时
			conn.setConnectTimeout(25000);
			// 读取超时 --服务器响应比较慢，增大时间
			conn.setReadTimeout(25000);
			HttpURLConnection.setFollowRedirects(true);
			// 请求方式
			conn.setRequestMethod("GET");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
			conn.setRequestProperty("Referer", WxStaticKeyUtil.WX_API_URL);
			conn.connect();
			// 获取URLConnection对象对应的输出流
			InputStream in = conn.getInputStream();
			BufferedReader read = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String valueString = null;
			while ((valueString = read.readLine()) != null) {
				bufferRes.append(valueString);
			}
			in.close();
			if (conn != null) {
				// 关闭连接
				conn.disconnect();
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return bufferRes.toString();
	}

}
